//
//  MyTabBar.swift
//  WangPu
//
//  Created by xiaozhi on 15/9/14.
//  Copyright (c) 2015年 zhixiao. All rights reserved.
//

import UIKit

class MyTabBar: UIView {
    let imageNomalArray:[String] = ["btn-1-n","btn-2-n","","btn-3-n","btn-4-n"]
    let imageSelectArray:[String] = ["btn-1-h","btn-2-h","","btn-3-h","btn-4-h"]
    let titleArray:[String] =   ["首页","分类","","购物车","我的"]
    var selectedBtn:MyButton?
    var shapeLayer:CAShapeLayer!
    var circleLayer:CAShapeLayer!
    let buttonWidth = windowWidth/5
    let buttonHeight = windowHeight
    let radiusOut:CGFloat = 30.0
    let circleWidth:CGFloat = (windowWidth-120)/5
    let circleHeight:CGFloat = (windowWidth-120)/5
    var middleButton:UIButton!
    var buttonEventHandle:((Int)->Void)?
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
//    override func drawRect(rect: CGRect) {
//        // Drawing code
//    }
    
    func  layoutButtons(){
//        var button:UIButton
        let buttonWidth = windowWidth/CGFloat(imageNomalArray.count)
        let buttonHeight = tabbarHeight
        for i in 0..<imageNomalArray.count{
            let button = MyButton(frame: CGRectMake(buttonWidth*CGFloat(i), 0, buttonWidth, buttonHeight))
            button.imageSize = CGSizeMake(23, 19)
            button.setTitle(titleArray[i], forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(11)
            button.titleLabel?.textAlignment = NSTextAlignment.Center
            button.setTitleColor(commonRedColor, forState: UIControlState.Selected)
            button.setTitleColor(UIColor(red: 128.0/255, green: 128.0/255, blue: 128.0/255, alpha: 1.0), forState: UIControlState.Normal)
            button.setImage(UIImage(named: imageNomalArray[i]), forState: UIControlState.Normal)
            button.setImage(UIImage(named: imageSelectArray[i]), forState: UIControlState.Selected)
            button.tag = i
            if i != 2{
                button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchDown)
                self.addSubview(button)
            }
            button.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
            if i == 0{
                self.buttonClick(button)
            }
        }
        middleButton =  UIButton(frame: CGRectMake(0, 0, 61, 61))
        middleButton.center = CGPointMake(self.center.x, tabbarHeight/2.0-4)
        middleButton.setTitle("旺", forState: UIControlState.Normal)
        middleButton.titleLabel?.font = UIFont.boldSystemFontOfSize(28)
        middleButton.titleLabel?.textColor = UIColor.whiteColor()
        middleButton.tag = 2
        self.addSubview(middleButton)
        

    }
    func buttonClick(button:UIButton){
        if button == selectedBtn{
            return
        }
        buttonEventHandle?(button.tag)
        button.selected = true
        selectedBtn?.selected = false
        selectedBtn = button as? MyButton
    }
    func selectIndex(index:NSInteger){
        if let button = self.viewWithTag(index) as? MyButton{
           self.buttonClick(button)
        }
        
    }
    func configShapeLayer(){
        shapeLayer = CAShapeLayer()
        shapeLayer.fillColor = backgroundColor?.CGColor
        shapeLayer.strokeColor = lightBackgoundColor.CGColor
        let scale = UIScreen.mainScreen().scale
        shapeLayer.lineWidth = 1
        shapeLayer.frame = bounds
        shapeLayer.lineJoin = kCALineJoinRound
        shapeLayer.lineCap = kCALineCapRound
        let path = UIBezierPath()
        let centerPoint = CGPointMake(self.center.x, tabbarHeight/2.0-4)
        path.addArcWithCenter(centerPoint, radius: circleWidth/2.0+8, startAngle: 0, endAngle: 2*CGFloat(M_PI), clockwise: true)
        path.closePath()
        path.addClip()
        shapeLayer.path = path.CGPath
        layer.addSublayer(shapeLayer)
        let coverShapeLayer = CAShapeLayer()
        coverShapeLayer.strokeColor = UIColor.clearColor().CGColor
        coverShapeLayer.fillColor = backgroundColor?.CGColor

//        coverShapeLayer.masksToBounds = true
        coverShapeLayer.lineWidth = 1
        var rect = bounds
        rect.origin.y -= 1
        coverShapeLayer.frame = CGRectMake(0, 1, bounds.size.width, bounds.size.height-1)
        coverShapeLayer.lineJoin = kCALineJoinRound
        coverShapeLayer.lineCap = kCALineCapRound
        let path0 = UIBezierPath()
        path0.moveToPoint(CGPointMake(0, 0))
        path0.addLineToPoint(CGPointMake(0, rect.size.height))
        path0.addLineToPoint(CGPointMake(rect.size.width, rect.size.height))
        path0.addLineToPoint(CGPointMake(rect.size.width, 0))
        path0.closePath()
        coverShapeLayer.path = path0.CGPath
        layer.addSublayer(coverShapeLayer)
        
    }
    func configCircleLayer(){
        circleLayer = CAShapeLayer()
        circleLayer.fillColor = UIColor.redColor().CGColor
        circleLayer.strokeColor = UIColor.clearColor().CGColor
        circleLayer.lineWidth = 1

        circleLayer.frame = CGRectMake(self.center.x-circleWidth/2.0, tabbarHeight/2.0-4-circleHeight/2.0, circleWidth, circleHeight)
        circleLayer.lineJoin = kCALineJoinRound
        circleLayer.lineCap = kCALineCapRound
        circleLayer.shadowColor = UIColor.blackColor().CGColor
        circleLayer.shadowOffset = CGSizeMake(1, 1)
        circleLayer.shadowOpacity = 0.6
        let path = UIBezierPath()
        let centerPoint = CGPointMake(circleWidth/2.0, circleHeight/2.0)
        path.addArcWithCenter(centerPoint, radius: circleWidth/2.0, startAngle: 0, endAngle: 2*CGFloat(M_PI), clockwise: true)
        path.closePath()
        circleLayer.path = path.CGPath
        layer.addSublayer(circleLayer)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.backgroundColor = UIColor(patternImage: UIImage(named: "tab-bg")!)
        backgroundColor = UIColor.whiteColor()
        let line = UIImageView(frame: CGRectMake(0, 0, bounds.size.width, 1))
        line.backgroundColor = lightBackgoundColor
        self.addSubview(line)

        self.configShapeLayer()
        self.configCircleLayer()
        self.layoutButtons()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
